/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef SWANLINK_FINANCE_KEYBOARD_UTILS_NATIVE_INCLUDE_KEYBOARD_COMMON_H
#define SWANLINK_FINANCE_KEYBOARD_UTILS_NATIVE_INCLUDE_KEYBOARD_COMMON_H

#include <cstdint>
#include <type_traits>
#include <string.h>

#include "hilog_wrapper.h"
#include "keyboard_errors.h"

namespace SwanLink {
namespace  Finance{
using namespace OHOS;
/**
 * @brief 服务ID 定义，正式项目需要移到系统定义里去
 * 
 */
// #define FINANCE_KEYBOARD_SERVICE_ID 30080

#define RETURN_IF_WITH_RET(cond, retval) if (cond) {return (retval);}
#define RETURN_IF(cond) if (cond) {return;}
#define RETURN_IF_WITH_LOG(cond, loginfo)            \
    do {  \
        if (cond) {\
            KEYBOARD_HILOGW(KEYBOARD_SRV, #loginfo); \
            return;\
        } \
    } while (0)    \

#define READ_PARCEL_NO_RET(parcel, type, out)    \
    do {  \
        if (!(parcel).Read##type(out)) {         \
            KEYBOARD_HILOGW(KEYBOARD_SRV, "read "#out" failed");   \
            return;\
        } \
    } while (0)    \

#define WRITE_PARCEL_NO_RET(parcel, type, data)  \
    do {  \
        if (!(parcel).Write##type(data)) {       \
            KEYBOARD_HILOGW(KEYBOARD_SRV, "write "#data" failed"); \
            return;\
        } \
    } while (0)    \

#define WRITE_BUFFER_NO_RET(parcel, type,  data, len)                              \
    do {                                                                               \
        if (!(parcel).WriteBuffer(data, len)) {                                             \
            KEYBOARD_HILOGW(KEYBOARD_SRV,  "write "#data" failed");                          \
            return;                                                           \
        }  \
    } while (0)

#define READ_PARCEL_WITH_RET(parcel, type, out, retval)  \
    do { \
        if (!(parcel).Read##type(out)) {        \
            KEYBOARD_HILOGW(KEYBOARD_SRV, "read "#out" failed");  \
            return (retval);                    \
        }  \
    } while (0)   \

#define READ_BUFFER_WITH_RET(parcel, type, out, len, retval)  \
    do { \
        out = (type*)(parcel).ReadBuffer(len); \
    } while (0)
    
#define READ_BUFFER_WITH_COPY(parcel, type, out,len, retval) \
    do { \
        memcpy(out, (const type*)(parcel).ReadBuffer(len), len); \
    } while (0)   \

#define WRITE_PARCEL_WITH_RET(parcel, type, data, retval)\
    do { \
        if (!(parcel).Write##type(data)) {      \
            KEYBOARD_HILOGW(KEYBOARD_SRV, "write "#data" failed");\
            return (retval);                    \
        }  \
    } while (0)

#define WRITE_BUFFER_WITH_RET(parcel, type, data,len, retval)\
    do { \
        if (!(parcel).WriteBuffer(data,len)) {      \
            KEYBOARD_HILOGW(KEYBOARD_SRV, "write "#data" failed");\
            return (retval);                    \
        }  \
    } while (0)

template<typename E>
constexpr auto ToUnderlying(E e) noexcept
{
    return static_cast<std::underlying_type_t<E>>(e);
}
} // namespace Finance
} // namespace SwanLink

#endif // SWANLINK_FINANCE_KEYBOARD_UTILS_NATIVE_INCLUDE_KEYBOARD_COMMON_H
